package br.com.fanor.view;

import br.com.fanor.job.MessageJob;
import br.com.fanor.listener.MessageListener;
import br.com.fanor.manager.FactoryManager;
import br.com.fanor.manager.MessageManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import org.jdesktop.layout.GroupLayout;

public class NewsLetterView extends javax.swing.JFrame {

    private final MessageManager messageManager = FactoryManager.getMessageManager();
    private final MessageJob messageJob;

    public NewsLetterView() {
        initComponents();
        this.setLocationRelativeTo(null);
        
        messageJob = new MessageJob();
        startMessageJob();
    }

    private void startMessageJob() {
        messageJob.setPriority(Thread.MIN_PRIORITY);
        messageJob.start();
    }

    @Override
    public void dispose() {
        messageJob.interrupt();
        super.dispose();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneBoxNewsletter = new JScrollPane();
        boxNewsletter = new JTextArea();
        btnSubscribeUnsubscribe = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Newsletter");
        setResizable(false);

        boxNewsletter.setEditable(false);
        boxNewsletter.setColumns(20);
        boxNewsletter.setRows(5);
        boxNewsletter.setBorder(null);
        jScrollPaneBoxNewsletter.setViewportView(boxNewsletter);

        btnSubscribeUnsubscribe.setText("Subscribe");
        btnSubscribeUnsubscribe.setHorizontalTextPosition(SwingConstants.CENTER);
        btnSubscribeUnsubscribe.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnSubscribeUnsubscribeMouseClicked(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(jScrollPaneBoxNewsletter)
            .add(GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(147, Short.MAX_VALUE)
                .add(btnSubscribeUnsubscribe)
                .add(150, 150, 150))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jScrollPaneBoxNewsletter, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btnSubscribeUnsubscribe)
                .add(0, 12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSubscribeUnsubscribeMouseClicked(MouseEvent evt) {//GEN-FIRST:event_btnSubscribeUnsubscribeMouseClicked
        if (messageManager.hasSubscribe(listener)) {
            messageManager.unsubscribe(listener);
            btnSubscribeUnsubscribe.setText("Subscribe");
        } else {
            messageManager.subscribe(listener);
            btnSubscribeUnsubscribe.setText("Unsubscribe");
        }
    }//GEN-LAST:event_btnSubscribeUnsubscribeMouseClicked
  
    private final MessageListener listener = new MessageListener() {
        @Override
        public void notify(String message) {
            boxNewsletter.setText(message);
        }
    };

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewsLetterView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewsLetterView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewsLetterView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewsLetterView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewsLetterView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JTextArea boxNewsletter;
    private JButton btnSubscribeUnsubscribe;
    private JScrollPane jScrollPaneBoxNewsletter;
    // End of variables declaration//GEN-END:variables
}
